<template>
  <div class="updata-name">
    <!-- 导航栏 -->
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onConfirm"
    />

    <div class="field-wrap">
      <!-- 输入框 -->
      <van-cell-group inset>
        <van-field
          v-model.trim="localName"
          rows="2"
          autosize
          label="留言"
          type="textarea"
          maxlength="7"
          placeholder="请输入留言"
          show-word-limit
        />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
import { updataUserProfile } from '@/api/user'
export default {
  name: 'UpdataName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  methods: {
    async onConfirm () {
      try {
        const localName = this.localName
        if (!localName.length) {
          this.$toast('昵称不能为空')
          return
        }
        await updataUserProfile({
          name: localName
        })
        this.$emit('input', localName)
        this.$emit('close')
        this.$toast.success('成功')
      } catch (err) {
        this.$toast.fail('失败')
      }
    }
  }
}
</script>

<style>
.filed-wrap {
  padding: 5px;
}
</style>
